<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程章节控制器
 */
class Chapter extends Backend
{
    protected $model = null;

    //设置关联查询
    protected $relationSearch = true;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Subject.Chapter');
    }

    //章节列表
    public function index($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $AuthWhere = ['subid' => $ids];

            $total = $this->model
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();


            $list = $this->model
                ->where($where)
                ->where($AuthWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 添加章节
    public function add($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['subid'] = $ids;

            if ($params) {
                $result = $this->model->validate("common/Subject/Chapter")->save($params);

                if ($result === FALSE) {
                    $this->error($this->model->getError());
                    exit;
                } else {
                    $this->success();
                    exit;
                }
            }
        }

        return $this->view->fetch();
    }

    // 编辑章节
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params['id'] = $ids;
                $params['subid'] = $row['subid'];
                $result = $this->model->validate("common/Subject/Chapter")->isUpdate(true)->save($params);

                if ($result === FALSE) {
                    if (!empty($params['url']) && $params['url'] != $row['url']) {
                        $video = substr($params['url'], 1);
                        @is_file($video) && @unlink($video);
                    }

                    $this->error($this->model->getError());
                    exit;
                } else {
                    if (!empty($params['url']) && $params['url'] != $row['url']) {
                        $video = substr($row['url'], 1);
                        @is_file($video) && @unlink($video);
                    }

                    $this->success();
                    exit;
                }
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    // 删除章节
    public function del($ids = null)
    {
        $list = $this->model->select($ids);

        if (empty($list)) {
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $video = array_column($list, "url");
            $video = array_filter($video);
            $video = array_unique($video);

            if (!empty($video)) {
                foreach ($video as $item) {
                    $item = ltrim($item, '/');
                    @is_file($item) && @unlink($item);
                }
            }

            $this->success();
            exit;
        }
    }
}
